package com.winit.dataInit;

import com.winit.Application;
import com.winit.config.model.Config;
import com.winit.config.service.IConfigService;
import com.winit.feature.model.Feature;
import com.winit.feature.model.FeatureReq;
import com.winit.feature.repository.FeatureRepository;
import com.winit.feature.repository.FeatureReqRepository;
import com.winit.feature.service.FeatureVersionGenerator;
import com.winit.feature.service.IFeatureService;
import com.winit.indexing.service.IIdexingService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.util.StringUtils;

import java.util.Iterator;
import java.util.Map;

@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = Application.class)
public class FeatureVersionInitializer {

    @Autowired
    private FeatureRepository featureRepository;
    @Autowired
    private IConfigService configService;
    @Autowired
    private FeatureReqRepository featureReqRepository;

//    @Test
    public void dealFeatureReqExp()throws Exception{
        String configId = "CFG2017122912533500002";
        Config config = this.configService.getConfigMap(configId, false);
        if (config == null){
            return;
        }
        Map<String, String> configMap = config.getKVMap();
        if (null == configMap.get("master-version")){
            return;
        }
        configMap.put("master_version", configMap.remove("master-version"));
        configMap.put("req_snapshot", configMap.remove("req-snapshot"));
        this.configService.saveConfig(config, false);
    }

//    @Test
    public void updatePrototypeVersionTag() throws Exception{
        Iterable<Feature> featuresIt = this.featureRepository.findAll();
        Iterator<Feature> featureIterator = featuresIt.iterator();
        Feature temp = null;
        while (featureIterator.hasNext()){
            temp = featureIterator.next();
            String configId = temp.getAttrConfigId();
            if (StringUtils.isEmpty(configId)){
                continue;
            }
            if (!"PROTOTYPE".equalsIgnoreCase(temp.getType())){
                continue;
            }
            Config masterConfig = this.configService.getConfigMap(configId, true);
            Map<String, String> masterConfigMap = masterConfig.getKVMap();
            if (null == masterConfigMap.get("master-version")){
                continue;
            }
            masterConfigMap.put("master_version", masterConfigMap.remove("master-version"));
            masterConfigMap.put("req_snapshot", masterConfigMap.remove("req-snapshot"));
            this.configService.saveConfig(masterConfig, false);
        }
    }

    @Test
    public void execute() throws Exception{
        Iterable<Feature> featuresIt = this.featureRepository.findAll();
        Iterator<Feature> featureIterator = featuresIt.iterator();
        Feature temp = null;
        while (featureIterator.hasNext()){
            temp = featureIterator.next();
            String configId = temp.getAttrConfigId();
            if (StringUtils.isEmpty(configId)){
                continue;
            }
            Config masterConfig = this.configService.getConfigMap(configId, true);
            Map<String, String> masterConfigMap = masterConfig.getKVMap();
            if (null != masterConfigMap.get("version")){
                continue;
            }
            this.updateVersionInfoForFeature(masterConfigMap, FeatureVersionGenerator.getVersionNo(), "", "");
            this.configService.saveConfig(masterConfig, false);
        }

        Iterable<FeatureReq> featureReqIterable = this.featureReqRepository.findAll();
        Iterator<FeatureReq> featureReqIterator = featureReqIterable.iterator();
        while (featureReqIterator.hasNext()){
            FeatureReq featureReq = featureReqIterator.next();
            String featureId = featureReq.getId().getFeatureId();
            String reqConfigId = featureReq.getAttrConfigId();
            Config reqConfig = this.configService.getConfigMap(reqConfigId, true);
            if (reqConfig == null){
                continue;
            }
            Map<String, String> reqConfigMap = reqConfig.getKVMap();
            if (null != reqConfigMap && (reqConfigMap.get("version")) != null){
                continue;
            }
            Feature relateFeature = this.featureRepository.findOne(featureId);
            if (relateFeature == null){
                continue;
            }
            String masterConfigId = relateFeature.getAttrConfigId();
            Config masterConfig = this.configService.getConfigMap(masterConfigId, true);
            Map<String, String> masterConfigMap = masterConfig.getKVMap();
            this.updateVersionInfoForFeature(reqConfigMap, FeatureVersionGenerator.getVersionNo(), masterConfigMap.get("version"), masterConfigMap.get("req_snapshot"));
            this.configService.saveConfig(reqConfig, false);
        }
    }

    private void updateVersionInfoForFeature(Map<String, String> configMap, String version, String masterVersion, String reqSnapshot){
        if (null != version) {
            configMap.put("version", version);
        }
        if (null != masterVersion) {
            configMap.put("master_version", masterVersion);
        }
        if (null != reqSnapshot) {
            configMap.put("req_snapshot", reqSnapshot);
        }
    }
}
